package com.autohelper.app.kit;

import android.annotation.SuppressLint;
import android.os.Build;
import android.provider.Settings;
import android.content.Context;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class ShortIdGenerator {

    public static String generateShortId(Context context) {
        // 获取 ANDROID_ID
        @SuppressLint("HardwareIds") String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);

        String combinedId = androidId;

        try {
            // 获取 SHA - 256 消息摘要实例
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            // 对拼接后的 ID 进行哈希计算
            byte[] hash = digest.digest(combinedId.getBytes());
            long hashLong = 0;
            for (int i = 0; i < Math.min(8, hash.length); i++) {
                hashLong <<= 8;
                hashLong |= (hash[i] & 0xff);
            }
            // 设定一个较大的模数，例如 1000000，生成 6 位以内的数字 ID
            return ((int) (Math.abs(hashLong) % 1000000)) + "";
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "设备号错误";
        }
    }
}